package com.yuerlive.rainbow.utils;

import android.app.Activity;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yuerlive.rainbow.RainbowApp;

import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * Created by liux on 2017/5/18.
 */

public class ThirdLoginUtils {
    private static volatile ThirdLoginUtils mInstance;
    InnerAuthorListener authListener;
    private ThirdLoginUtils() {
    }

    public static ThirdLoginUtils getInstance() {
        if (mInstance == null) {
            synchronized (ThirdLoginUtils.class) {
                if (mInstance == null) {
                    mInstance = new ThirdLoginUtils();
                }
            }
        }
        return mInstance;
    }

    public void setListener(UserLoginInfoListenter listener) {
        this.authListener = new InnerAuthorListener(listener);
    }

    public void removeListener() {
        UMShareAPI.get(RainbowApp.getContext()).release();
    }

    public void ThridLogin( SHARE_MEDIA platform) {
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(RainbowApp.getContext()).setShareConfig(config);
        UMShareAPI.get(RainbowApp.getContext()).getPlatformInfo((Activity) authListener.weakReference.get(), platform, authListener);
    }




    static class InnerAuthorListener implements UMAuthListener {
        private WeakReference<UserLoginInfoListenter> weakReference;

        public InnerAuthorListener(UserLoginInfoListenter listenter) {
            weakReference = new WeakReference<UserLoginInfoListenter>(listenter);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {
            L.e("ThirdLoginUtils", "onStart");
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            L.e("ThirdLoginUtils", "onComplete");
            String temp = "";
            for (String key : data.keySet()) {
                temp = temp + key + " : " + data.get(key) + "\n";
            }
            //Todo listener 为空  看看该怎么处理
            if (weakReference.get() != null) {
                weakReference.get().setLoginInfo(data, platform);
                weakReference.get().hideProgressBar();
            }
            L.e("ThirdLoginUtils", "temp" + temp + "\n" + "action" + action + ":::" + "platform" + platform);
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            L.e("ThirdLoginUtils", "onError");
            ToastUtils.show("登录错误");
            if (weakReference.get() != null) {
                weakReference.get().hideProgressBar();
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            L.e("ThirdLoginUtils", "onCancel");
            ToastUtils.show("登录取消");
            if (weakReference.get() != null) {
                weakReference.get().hideProgressBar();
            }
        }
    }

    public  interface UserLoginInfoListenter {
        void setLoginInfo(Map<String, String> data, SHARE_MEDIA platform);

        void hideProgressBar();
    }

}
